
// 1.1 获取裁剪区域的 DOM 元素
var $image = $('#image')

// 1.2 配置选项
const options = {
  // 纵横比
  aspectRatio: 1,
  // 指定预览区域
  preview: '.img-preview'
}

// 1.3 创建裁剪区域
$image.cropper(options)
$('#btnChooseImage').click(function () {
  $('#file').click()
})
$('#file').on('change', function (e) {
  var flieslist = e.target.files;
  console.log(flieslist);
  if (flieslist.length == 0) {
    return layer.msg('上传失败')
  }
  var imgURL = URL.createObjectURL(flieslist[0]);
  $image.cropper('destroy').attr('src', imgURL).cropper(options)
});

$('#btnUpload').on('click', function () {
  var dataURL = $image.cropper('getCroppedCanvas', {
    width: 100,
    height: 100
  }).toDataURL('image/png');
  $.ajax({
    method: 'POST',
    url: '/my/update/avatar',
    data: {
      avatar: dataURL
    },
    success: function (res) {
      if (res.status !== 0) {
        return layer.msg('更换头像失败！')
      }
      layer.msg('更换头像成功！')
      window.parent.gets();
    }
  })
})